/**
 * 
 */
function InterFormFiller(){
	this.isChanged = false;
	this.m_pdPage = null;
	this.objList = [];
	var _self = this;
	this.m_id = 0;
	//控件的创建
	this.CreateControl = function(pdPage,matrix){
		//先要创建一个form表单
		this.m_pdPage = pdPage;
	    var m_controlList = this.m_pdPage.GetFormControlList();
		for(var i = 0;i < m_controlList.length;i++){
			var type = m_controlList[i].m_nType;
			if(type == 1){//PushButton
				_self.CreateButton(m_controlList[i],matrix);
			}else if(type == 2){//RadioButton
				_self.CreateButton(m_controlList[i],matrix);
			}else if(type == 3){//CheckBox
				_self.CreateButton(m_controlList[i],matrix);
			}else if(type == 4){//Text
				var pdDoc = pdPage.GetPDDoc();
				var interForm = pdDoc.m_interForm;
				_self.CreateText(m_controlList[i],interForm,matrix);
			}else if(type == 5){//RichText
				var pdDoc = pdPage.GetPDDoc();
				var interForm = pdDoc.m_interForm;
				_self.CreateText(m_controlList[i],interForm,matrix);
			}else if(type == 6){//File
				var pdDoc = pdPage.GetPDDoc();
				var interForm = pdDoc.m_interForm;
				_self.CreateText(m_controlList[i],interForm,matrix);
			}else if(type == 7){//ListBox
				_self.CreateChoice(m_controlList[i],matrix);
			}else if(type == 8){//ComboBox
				_self.CreateChoice(m_controlList[i],matrix);
			}else if(type == 9){//Sign
				_self.CreateSignature(m_controlList[i]);
			}else{//Unknown
				
			}
			_self.m_id++;
		}
	};
	
	this.CreateButton = function(controlData,matrix){
		var fieldId = controlData.m_nFieldID;
		var interFormList = _self.m_pdPage.GetPDDoc().m_interForm;
		var fieldData = interFormList.getFieldByid(fieldId);
		var button = new Button(_self.m_id,_self.m_pdPage.GetIndex());
		var annotId = controlData.m_nAnnotID;
		var annotList = _self.m_pdPage.GetAnnotList();
		var annot = annotList.getAnnotDataById(annotId);
		button.SetStyle(annot,matrix,controlData,fieldData);
		button.CreateButton(controlData.m_nType);
		var listener = new ListenerObj();
		listener.Action(function(){
			$("#" + button.GetId());
		});
		button.SetListener(listener);
		_self.objList.push(button);
	};
	
	this.CreateText = function(controlData,interForm,matrix){
		var text = new Text(_self.m_id,_self.m_pdPage.GetIndex());
		var annotId = controlData.m_nAnnotID;
		var annotList = _self.m_pdPage.GetAnnotList();
		var annot = annotList.getAnnotDataById(annotId);
		var pos = matrix.TransFormRect(parseInt(annot.m_fRect[0]),parseInt(annot.m_fRect[1]),parseInt(annot.m_fRect[2]),parseInt(annot.m_fRect[3]));
		if(pos[0] == 0){
			return;
		}
		text.SetStyle(annot,matrix);
		text.CreateText();
		var listener = new ListenerObj();
		listener.Action(function(){
			
		});
		text.SetListener(listener);
		_self.objList.push(text);

		//box
		
		//space
	};
	
	this.CreateChoice = function(controlData,matrix){
		var fieldId = controlData.m_nFieldID;
		var interFormList = _self.m_pdPage.GetPDDoc().m_interForm;
		var fieldData = interFormList.getFieldByid(fieldId);
		var choice = new Choice(_self.m_id,_self.m_pdPage.GetIndex());
		var annotId = controlData.m_nAnnotID;
		var annotList = _self.m_pdPage.GetAnnotList();
		var annot = annotList.getAnnotDataById(annotId);
		choice.SetStyle(annot, matrix,fieldData.m_addEntries.m_arrOpt);
		choice.CreateChoice(controlData.m_nType);
		var listener = new ListenerObj();
		listener.Action(function(){
			
		});
		choice.SetListener(listener);
		_self.objList.push(choice);
	};
	
	this.CreateSignature = function(controlData){
		
	};
	
	//控件监听响应
	this.ControlListener = function(){
		//BtnListener
		
		//TxListener
		
		//ChListener
	
		//SigListener
	};
	
	this.MouseListener = function(){
		
	};
	//判断是否有数据的改变
	this.IsChanged = function(){
		
	};
	//数据保存
	this.DataSave = function(){
		
	};
}

function ListenerObj(){
	this.Action = function(){
	
	};
};
//obj 对象
function Button(id,pageIndex){
	this.m_id = "MainFrame_ViewContainer_BG_" + pageIndex + "_form_" + id + "_fg";
	this.m_inputId = null;
	var _self = this;
	this.m_top = 0;
	this.m_left = 0;
	this.m_pageIndex = pageIndex;
	this.m_right = 0;
	this.m_bottom = 0;
	this.m_width = 0;
	this.m_height = 0;
	this.m_opt = [];
	this.m_listener = null;
	this.m_checked = false;
	this.m_name = null;
	
	this.GetId = function(){
		return _self.m_inputId;
	};
	
	this.SetStyle = function(annot,matrix,controlData,fieldData){
		var pos = matrix.TransFormRect(parseInt(annot.m_fRect[0]),parseInt(annot.m_fRect[1]),parseInt(annot.m_fRect[2]),parseInt(annot.m_fRect[3]));
		_self.m_top = pos[3];
		_self.m_left = pos[0];
		_self.m_width = Math.abs(pos[2] - pos[0]) - 2;
		_self.m_height = Math.abs(pos[3] - pos[1]) - 3;
		_self.m_checked = controlData.m_bChecked;
		_self.m_name = fieldData.m_strFullName;
	};
	
	this.CreateButton = function(type){
		var str = "<div id=\"" + _self.m_id + "\"><div class=\"fwr_form_field_highlight fwr_form_widget\" style=\"left: " + (_self.m_left + 2)+ "px; top: " + (_self.m_top + 1) + "px; width: " + _self.m_width + "px; height: " + _self.m_height + "px; position: absolute; padding: 0px; cursor: pointer;\"></div></div>";
		$("#canvas" + _self.m_pageIndex).parent().append(str);
		
		switch(type){
			case 1:
				//str = "<input type=\"button\" style=\"left: " + (_self.m_left + 2)+ "px; top: " + (_self.m_top + 1) + "px; width: " + _self.m_width + "px; height: " + _self.m_height + "px; position: absolute; padding: 0px; cursor: pointer;\"></input>";
				break;
			case 2:
				_self.m_inputId = _self.m_id + "radio";
				var checkstr = _self.m_checked == "1" ? "checked=\"checked\"" : "";
				str = "<input id=\"" + _self.m_inputId + "\" type=\"radio\" "+ checkstr + " name=\"" + _self.m_name + "\" style=\"left: " + (_self.m_left - 5)+ "px; top: " + (_self.m_top - 5) + "px; width: " + _self.m_width + "px; height: " + _self.m_height + "px; position: absolute; padding: 0px; cursor: pointer;\"></input>";
				break;
			case 3:
				_self.m_inputId = _self.m_id + "check";
				var checkstr = _self.m_checked == "1" ? "checked=\"checked\"" : "";
				str = "<input id=\"" + _self.m_inputId + "\" type=\"checkbox\" "+ checkstr + " group=\"" + _self.m_name + "\" style=\"left: " + (_self.m_left - 5)+ "px; top: " + (_self.m_top - 5) + "px; width: " + _self.m_width + "px; height: " + _self.m_height + "px; position: absolute; padding: 0px; cursor: pointer;\"></input>";
				break;
		}
		$("#" + _self.m_id).append(str);
	};
	this.SetListener = function(listener){
		this.m_listener = listener;
		this.m_listener.Action();
	};
}

function Text(id,pageIndex){
	this.m_id = "MainFrame_ViewContainer_BG_" + pageIndex + "_form_" + id + "_bg_div";
	this.m_listener = null;
	var _self = this;
	this.m_top = 0;
	this.m_left = 0;
	this.m_right = 0;
	this.m_bottom = 0;
	this.m_width = 0;
	this.m_height = 0;
	this.m_pageIndex = pageIndex;
	this.m_color = null;
	this.m_lineWidth = null;
	this.m_HightLight = false;
	this.SetListener = function(listener){
		this.m_listener = listener;
	};
	this.SetStyle = function(annot,matrix){
		var pos = matrix.TransFormRect(parseInt(annot.m_fRect[0]),parseInt(annot.m_fRect[1]),parseInt(annot.m_fRect[2]),parseInt(annot.m_fRect[3]));
		//位置
		_self.m_top = pos[3];
		_self.m_left = pos[0];
		_self.m_width = Math.abs(pos[2] - pos[0]) - 2;
		_self.m_height = Math.abs(pos[3] - pos[1]) - 3;
		//颜色
		_self.m_color = _self.GetColor(annot.m_color);
		//边框线宽
		_self.m_lineWidth = annot.m_structBS.m_nWidth;
		//是否高亮显示
		_self.m_HightLight = annot.m_AddEntries.m_strHighlightMode == "N" ? true : false;
	};
	this.GetColor = function(colorArr){
		var red = Math.round((colorArr[0] == null || colorArr[0] == "undefined" ? 0 : colorArr[0]) * 255);
		var green = Math.round((colorArr[1] == null || colorArr[1] == "undefined" ? 0 : colorArr[1]) * 255);
		var blue = Math.round((colorArr[2] == null || colorArr[2] == "undefined" ? 0 : colorArr[2]) * 255);
		
		return "rgb(" + red + "," + green + "," + blue + ")";
	};
	this.CreateText = function(){
//		if(_self.m_HightLight){
			var str = "<div id=\"" + _self.m_id + "\"><div  class=\"fwr_form_field_highlight fwr_form_widget\" style=\"position:absolute;top:" + _self.m_top + "px;left:" + _self.m_left + "px;height:" + _self.m_height + "px;width:" + _self.m_width + "px;border:" + 1  + "px solid;cursor: text;overflow:hidden;\"></div></div>";
			$("#canvas" + _self.m_pageIndex).parent().append(str);
//		}
		
		var str = null;
		if(_self.m_height < 20){
			str = "<input class=\"fwr_form_widget\" type=\"text\" value=\"\" multiline=\"0\" style=\"left:" + (_self.m_left + 2) + "px;top:" + _self.m_top + "px;width:" + _self.m_width + "px;height:" + _self.m_height + "px;line-height:" + _self.m_height +"px;position:absolute;font-size:12px;background-image:url(about:blank);background-color:transparent;border:0;padding:0;\"></input>";
		}else{
			str = "<textarea class=\"fwr_form_widget fwr_form_textarea_none_border\" multiline=\"1\" style=\"left:" + (_self.m_left + 2) + "px;top:" + _self.m_top + "px;width:" + _self.m_width +"px;height:" + _self.m_height + "px;line-height:100%;max-width:" + _self.m_width + "px;max-height:" + _self.m_height +"px;position:absolute;background-image:url(about:blank);background-color:transparent;font-size:12px;resize:none;border:0;padding:0;\"></textarea>";
		}
		$("#" + _self.m_id).append(str);
	};
}

function Choice(id,pageIndex){
	this.m_id = "MainFrame_ViewContainer_BG_" + pageIndex + "_form_" + id + "_bg_div";
	this.m_listener = null;
	var _self = this;
	this.m_top = 0;
	this.m_left = 0;
	this.m_right = 0;
	this.m_bottom = 0;
	this.m_width = 0;
	this.m_height = 0;
	this.m_pageIndex = pageIndex;
	this.m_selected = [];
	this.m_fontSize = 8;
	this.SetListener = function(listener){
		this.m_listener = listener;
	};
	
	this.SetStyle = function(annot,matrix,selectText){
		var pos = matrix.TransFormRect(parseInt(annot.m_fRect[0]),parseInt(annot.m_fRect[1]),parseInt(annot.m_fRect[2]),parseInt(annot.m_fRect[3]));
		//位置
		_self.m_top = pos[3];
		_self.m_left = pos[0];
		_self.m_width = Math.abs(pos[2] - pos[0]);
		_self.m_height = Math.abs(pos[3] - pos[1]) - 3;
		
		_self.m_selected = selectText;
	};
	
	this.CreateChoice = function(type){
		var str = "<div id=\"" + _self.m_id + "\"><div  class=\"fwr_form_field_highlight fwr_form_widget\" style=\"position:absolute;top:" + _self.m_top + "px;left:" + _self.m_left + "px;height:" + _self.m_height + "px;width:" + _self.m_width + "px;border:" + 1  + "px solid;cursor: text;overflow:hidden;\"></div></div>";
		$("#canvas" + _self.m_pageIndex).parent().append(str);
		
		switch(type){
			case 7:
				break;
			case 8:
				var opt = "";
				for(var i = 0;i < _self.m_selected.length;i+=2){
					opt += "<option value=\"" + _self.m_selected[i + 1] + "\">" + _self.m_selected[i] + "</option>";
				}
				str = "<select class=\"fwr_form_field_highlight fwr_form_widget\" style=\"position:absolute;top:" + _self.m_top + "px;left:" + _self.m_left + "px;height:" + _self.m_height + "px;width:" + _self.m_width + "px;border:" + 1  + "px solid;cursor: text;overflow:hidden;font-size:" + _self.m_fontSize + "px;\">"
						+ opt
						+ "</select>";
				break;
		}
		
		$("#" + _self.m_id).append(str);
	};
}

function Signature(){
	this.m_listener = null;
	
	this.SetListener = function(listener){
		this.m_listener = listener;
	};
}